package com.luomu.base.biz.cookBook.excel.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luomu.base.api.bo.RspBaseBO;
import com.luomu.base.api.bo.RspPage;
import com.luomu.base.api.constant.BaseRspConstants;
import com.luomu.base.api.cookBook.excel.service.CommonExcelExportService;
import com.luomu.base.api.cookBook.excel.bo.*;
import com.luomu.base.api.cookBook.excel.contant.ExportResultStatus;
import com.luomu.base.api.exception.ZTBusinessException;
import com.luomu.base.api.utils.AssignBasicFieldUtil;
import com.luomu.base.biz.cookBook.excel.dao.ExportFileCtlLogMapper;
import com.luomu.base.biz.cookBook.excel.dao.ExportFileMapper;
import com.luomu.base.biz.cookBook.excel.po.ExportFileCtlLogPO;
import com.luomu.base.biz.cookBook.excel.po.ExportFilePO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yanhan
 * @since 2024-11-29
 * @version 1.0
 * @Description 公共导出接口。调用   exportExcel()
 */

@RestController
@RequestMapping("/commonExcelExportService")
public class CommonExcelExportServiceImpl implements CommonExcelExportService {

	protected Logger logger = LoggerFactory.getLogger(CommonExcelExportServiceImpl.class);

	@Autowired
	private ExportFileMapper exportFileMapper;

	@Autowired
	private ExportFileCtlLogMapper exportFileCtlLogMapper;

	@Autowired
	private List<AbstractCommonExcelExportStrategy> abstractCommonExcelExportStrategy;

	@Override
	public RspBaseBO export(@RequestBody CommonExcelExportReq commonExcelExportReq) {

		AbstractCommonExcelExportStrategy commonExcelExportStrategy = null;
		for (AbstractCommonExcelExportStrategy item : abstractCommonExcelExportStrategy){
			if(item.getOriginType().equals(commonExcelExportReq.getOriginType())){
				commonExcelExportStrategy = item;
				break;
			}
		}

		if(commonExcelExportStrategy == null){
			throw new ZTBusinessException(BaseRspConstants.RSP_CODE_InvalidParameter, BaseRspConstants.RSP_DESC_InvalidParameter);
		}

		ExportFilePO po = new ExportFilePO();
		AssignBasicFieldUtil.copyAssignBasicField(commonExcelExportReq, po, false);

		po.setCreateUserId(commonExcelExportReq.getOpenId());
		po.setCreateUserName(commonExcelExportReq.getPersonName());
		po.setOriginType(commonExcelExportReq.getOriginType());
		po.setRequestParam(commonExcelExportReq.getRequestParam());
		po.setFileName(commonExcelExportStrategy.getFileName(commonExcelExportReq.getRequestParam()));
		po.setStatus(ExportResultStatus.WAIT.getVal());
		exportFileMapper.add(po);

		AbstractCommonExcelExportStrategy finalCommonExcelExportStrategy = commonExcelExportStrategy;
		new Thread(() -> {
			Long fileId = po.getFileId();
			try {
				CommonExportResultBO resultBO = finalCommonExcelExportStrategy.handler(commonExcelExportReq, po);
				exportFileMapper.updateDataById(fileId, resultBO);
			}catch (Exception e){
				logger.error("导出失败", e);
				exportFileMapper.updateDataById(fileId, new CommonExportResultBO(ExportResultStatus.FAIL.getVal()
						, e.getMessage()));
			}
		}).start();
		return BaseRspConstants.returnSuccess();
	}

	@Override
	public RspPage<CommonExportListDataRspBO> queryCommonExcelExport(@RequestBody CommonExportListDataReqBO reqBO){
		RspPage<CommonExportListDataRspBO> rspPage = new RspPage<>();
		Page<CommonExportListDataRspBO> page = new Page<>(reqBO.getPageNo(), reqBO.getPageSize());
		List<CommonExportListDataRspBO> data = exportFileMapper.queryList(reqBO, page);
		rspPage.setPageNo(reqBO.getPageNo());
		rspPage.setRows(data);
		rspPage.setTotal(page.getTotal());
		rspPage.setCode(BaseRspConstants.RSP_CODE_SUCCESS);
		rspPage.setMessage(BaseRspConstants.RSP_DESC_SUCCESS);
		return rspPage;
	}

	@Override
	public RspBaseBO delete(@RequestBody CommonExcelExportOperationReq req){
		ExportFilePO exportFilePO = exportFileMapper.selectById(req.getFileId());
		if(exportFilePO == null || ExportResultStatus.WAIT.getVal().equals(exportFilePO.getStatus())){
			throw new ZTBusinessException(BaseRspConstants.RSP_CODE_InvalidParameter, BaseRspConstants.RSP_DESC_InvalidParameter);
		}
		ExportFileCtlLogPO po = new ExportFileCtlLogPO();
		AssignBasicFieldUtil.copyAssignBasicField(req, po, false);
		po.setCreateUserId(req.getOpenId());
		po.setCreateUserName(req.getPersonName());
		po.setFileId(exportFilePO.getFileId());
		po.setFileName(exportFilePO.getFileName());
		po.setType(2);
		exportFileCtlLogMapper.add(po);
		exportFileMapper.deleteDataById(req.getFileId());
		return BaseRspConstants.returnSuccess();
	}

	@Override
	public RspBaseBO download(@RequestBody CommonExcelExportOperationReq req){
		ExportFilePO exportFilePO = exportFileMapper.selectById(req.getFileId());
		if(exportFilePO == null || !ExportResultStatus.SUCCESS.getVal().equals(exportFilePO.getStatus())){
			throw new ZTBusinessException(BaseRspConstants.RSP_CODE_InvalidParameter, BaseRspConstants.RSP_DESC_InvalidParameter);
		}
		ExportFileCtlLogPO po = new ExportFileCtlLogPO();
		AssignBasicFieldUtil.copyAssignBasicField(req, po, false);
		po.setCreateUserId(req.getOpenId());
		po.setCreateUserName(req.getPersonName());
		po.setFileId(exportFilePO.getFileId());
		po.setFileName(exportFilePO.getFileName());
		po.setType(1);
		exportFileCtlLogMapper.add(po);
		return BaseRspConstants.returnSuccess();
	}

}